package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.DeptDao;
import com.gmgx.hrms.entity.Dept;
import com.gmgx.hrms.util.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
   private JDBCTools tools=new JDBCTools();
    @Override
    public Dept queryDeptById(Integer id) {
        Dept dept=null;
       ResultSet rs= tools.query("select * from t_dept where dept_id=?",id);
        try {
            if(rs.next()){
                dept=new Dept();
                dept.setDept_id(rs.getInt("dept_id"));
                dept.setDept_addr(rs.getString("dept_addr"));
                dept.setDept_tel(rs.getString("dept_tel"));
                dept.setDept_name(rs.getString("dept_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return dept;
    }

    @Override
    public List<Dept> queryAllDept() {
        List<Dept> list=new ArrayList<>();
        ResultSet rs= tools.query("select * from t_dept ");
        try {
            while(rs.next()){
                Dept  dept=new Dept();
                dept.setDept_id(rs.getInt("dept_id"));
                dept.setDept_addr(rs.getString("dept_addr"));
                dept.setDept_tel(rs.getString("dept_tel"));
                dept.setDept_name(rs.getString("dept_name"));
                list.add(dept);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }
}
